﻿using Bussiness;
using Model;
using System;
using System.Windows.Forms;
using Common;
using CCWin;

namespace Scada
{
    public partial class FrmDevPOIAdd : Skin_Mac
    {
        public string _DevPOIGuid;
        BLL _BLL = new BLL();
        public event ONBindDevicePOIListHandler ONBindDevicePOIList;
        public FrmDevPOIAdd()
        {
            InitializeComponent();
        }

        private void FrmDevPOIAdd_Load(object sender, EventArgs e)
        {
            cbx设备类型.FieldList = _BLL._DAL.GetDeviceTypeFieldList();
            cbx设备类型.Bind();
            cbx点位类型.FieldList = _BLL._DAL.GetDevicePOITypeFieldList();
            cbx点位类型.Bind();
            this.cbx是否采集.SelectedItem = "是";
            DevicePOI devicePOI = _BLL._DAL.GetDevicePOI(_DevPOIGuid);
            if (devicePOI != null)
            {
                this.cbx设备类型.SelectedValue = devicePOI.DevTypeGuid;
                this.txt点位名称.Text = devicePOI.DevPOIName;
                this.txt点位别名.Text = devicePOI.DevPOIAlias;
                this.cbx点位类型.SelectedValue = devicePOI.DevPOIType;
                this.txt点位地址.Text = devicePOI.Address;
                this.cbx是否采集.SelectedItem = devicePOI.Enable ? "是" : "否";
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string devTypeGuid = this.cbx设备类型.SelectedValue.ToString();
            string devPOIName = this.txt点位名称.Text.Trim();
            string devPOIAlias = this.txt点位别名.Text.Trim();
            string devPOIType= this.cbx点位类型.SelectedValue.ToString();
            string address = this.txt点位地址.Text.Trim();
            object enableobj = this.cbx是否采集.SelectedItem;

            if (string.IsNullOrEmpty(devTypeGuid))
            {
                MessageBox.Show("设备类型未选！");
                return;
            }

            if (string.IsNullOrEmpty(devPOIName))
            {
                MessageBox.Show("点位名称空！");
                return;
            }

            if ("5".Equals(devPOIType))
            {
                if (!address.Contains("_"))
                {
                    MessageBox.Show("字符串类型地址格式为地址_长度。如D1000_20！");
                    txt点位地址.Focus();
                    return;
                }
            }else if ("6".Equals(devPOIType))
            {
                if (!address.Contains("."))
                {
                    MessageBox.Show("位类型地址格式为地址.位索引。如D1000.2！");
                    txt点位地址.Focus();
                    return;
                }
            }

            DevicePOI devicePOI = new DevicePOI();
            devicePOI.Guid = string.IsNullOrEmpty(_DevPOIGuid) ? Guid.NewGuid().ToString() : _DevPOIGuid;
            devicePOI.DevTypeGuid = devTypeGuid;
            devicePOI.DevPOIName = devPOIName;
            devicePOI.DevPOIAlias = devPOIAlias;
            devicePOI.DevPOIType = devPOIType.ToInt();
            devicePOI.Address = address;
            devicePOI.Enable = enableobj.ToString().Equals("是") ? true : false;
            Result result = _BLL._DAL.SaveDevicePOI(devicePOI, string.IsNullOrEmpty(_DevPOIGuid) ? true : false);
            if (result.Success)
            {
                MessageBox.Show("成功！");
                ONBindDevicePOIList(string.Empty);
                this.Close();
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
